/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-08-08 15:54:09
 * @LastEditors: lyy 2684842688@qq.com
 * @LastEditTime: 2022-09-23 17:58:02
 * @FilePath: \project\pyy\src\utils\axios.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";
import { BASEURL, ACCESS_TOKEN } from "../http/apiUrl";
const myAxios = axios.create({
  //axios的url基础路径
  baseURL: BASEURL,
  //时间限制，超过这个时间，会直接报错，用于请求超时的时间
  timeout: 30000,
});
//axios拦截器
//请求拦截器，拦截请求，拦截成功执行第一个函数，否则第二个
myAxios.interceptors.request.use(
  (config) => {
    // 添加公共的token
    if (!config.url.includes("webjars"))
      config.params = {
        // access_token: ACCESS_TOKEN,
        access_token: localStorage.frameToken,//上线用xxxx
      };
    return config;
  },
  (err) => {
    return err;
  }
);

//axios响应拦截器
myAxios.interceptors.response.use(
  (res) => {
    return res;
  },
  (err) => {
    //响应失败时执行
    let message = "响应服务器失败";
    console.log(err, 989);
    if (err && err.response) {
      console.log(err.response);
      switch (err.response.status) {
        case 401:
          message = "身份认证失败（401）";
          alert("未登录，请先登录");
          break;
        case 404:
          message = "请求资源找不到（404）";
          break;
        case 500:
          message = "服务器错误（500）";
          break;
        case 504:
          message = "网络超时（504）";
          break;
      }
      return {
        data: {
          message,
          msg: message,
          type: err.response.status,
          status: 0,
        },
      };
    } else {
      return {
        data: {
          message: "服务器连接失败！",
          msg: message,
          status: 0,
        },
      };
    }
  }
);
export default myAxios;
